home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
tools
/
classdoc.lha
/
classdoc
/
list.H
< prev
Wrap
C/C++ Source or Header
|
1993-08-08
|
3KB
|
125 lines
// Generic list package in C++. Any kind of object can be the member of
// such a list; only pointers to elements are stored. An element can be
// the member of many lists at a time. Before an object is destructed,
// it must explicitly be taken out of any lists (dangling pointer problem).
//
// This is a protected class, i.e., it is not possible to create instances
// of it. Derived classes should provide lists for specific types of objects.
//
// Implementation note. The list is implemented as an array of pointers.
// Inserting elements at the front of the list (Insert() and operator +=)
// is cheap; removing elements (except the first) and inserting at the end
// is more expensive. Assignment and passing a list as a VALUE parameter
// requires copying of the pointer array, and is therefore expensive; pass
// lists as "const reference" parameters instead.
//
// Author: Dag Bruck. Date: 1989-07-14.
#ifndef LIST_H
#define LIST_H
#include "defs.H"
#include "generic.h"
typedef void* Pointer;
class GenericList {
friend class GenericIterator;
public:
GenericList();
// Constructs a list with no elements.
~GenericList();
// Destructs list. The elements in the list are not
// destructed.
void Insert(Pointer);
void operator += (Pointer);
// Inserts an element first in list.
void Append(Pointer);
// Inserts an element last in list. Not as efficient as Insert().
void Remove(Pointer);
// Removes an element from list. The element is not destructed.
boolean Member(Pointer);
// Returns true if the element is a member of list.
void Reverse();
// Reverses the order of the elements in the list. The list is reversed
// in place, i.e., the original order is lost.
Pointer First();
// Returns the first element of list, or nil if list is empty.
Pointer Last();
// Returns the last element of list, or nil if list is empty.
unsigned Length();
// Returns the number of elements in list.
boolean Empty();
// Returns true if list is empty.
GenericList(const GenericList&);
// Used for passing parameters and return values.
void operator = (const GenericList&);
// Assignment.
private:
Pointer* p;
// Array of pointers to actual member objects.
unsigned n;
// Number of elements in list.
unsigned size;
// Maximum number of elements in list.
void Expand();
// Expand array of pointers to cope with more elements.
};
inline void GenericList :: operator += (Pointer e)
{ Insert(e); }
inline unsigned GenericList :: Length()
{ return n; }
inline boolean GenericList :: Empty()
{ return n == 0; }
// Ugly stuff to fake parameterized types.
#define LIST(type)name2(type,List)
#define LIST_TYPE(type) class LIST(type) : public GenericList { \
public: \
LIST(type)() {} \
~LIST(type)() {} \
void Insert(type& x) { GenericList::Insert(&x); } \
void Insert(type* x) { GenericList::Insert(x); } \
void operator += (type& x) { GenericList::operator += (&x); } \
void operator += (type* x) { GenericList::operator += (x); } \
void Append(type& x) { GenericList::Append(&x); } \
void Append(type* x) { GenericList::Append(x); } \
void Remove(type& x) { GenericList::Remove(&x); } \
void Remove(type* x) { GenericList::Remove(x); } \
boolean Member(type& x) { return GenericList::Member(&x); } \
boolean Member(type* x) { return GenericList::Member(x); } \
type* First() { return (type *) GenericList::First(); } \
type* Last() { return (type *) GenericList::Last(); } \
}
#endif